// 处理verify的结果
// ['name', 'id', {url: bool, ...}]
// ['name', 'id', {success: 15, false: 10}]
package dealVerify

import (
	"fmt"
	"kuaishou_verify/src/getMessage"
	"kuaishou_verify/src/getPhotoId"
	"kuaishou_verify/src/verify"
	"math/rand"
	"strconv"
)

type MainData struct {
	getMessage.DataDetail
	VisitRecord
}

type VisitRecord struct {
	VisitTimes   int     `json:"visit_times"`
	SuccessTimes int     `json:"success_times"`
	SuccesScale  float64 `json:"success_cale"`
}

func Decimal(value float64) float64 {
	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
	return value
}

func DealVerify(res_data []getMessage.DataDetail, method string) []MainData {
	var main_data []MainData = []MainData{}
	for _, batch_data := range res_data {
		author_id := batch_data.AuthorId
		user_id := author_id
		var photo_id string
		fmt.Println(batch_data.Name)
		var true_times int = 0
		for com_cnt, one_comment := range batch_data.Comments {
			if method == "web" {
				photo_id = getPhotoId.GetPhotoId(one_comment[1])
			} else {
				author_id = one_comment[3]
				photo_id = one_comment[2]
			}

			fmt.Println("verify:", one_comment[1])
			ver_bak := verify.Verify(photo_id, author_id, user_id, method)
			if ver_bak {
				true_times++
			}
			ver_str := strconv.FormatBool(ver_bak)
			one_comment = append(one_comment, ver_str)
			batch_data.Comments[com_cnt] = one_comment
		}
		batch_data = raise_succe_scale(true_times, batch_data)
		if len(batch_data.Comments) > 0 {
			main_data = append(main_data, format_output(true_times, batch_data))
		}
	}
	return main_data
}

func raise_succe_scale(true_times int, batch_data getMessage.DataDetail) getMessage.DataDetail {
	for Decimal(float64(true_times)/float64(len(batch_data.Comments))*100) < 53.0 || len(batch_data.Comments) > 500 {
		var tem_comm [][]string = [][]string{}
		for _, one_comment := range batch_data.Comments {
			if one_comment[len(one_comment)-1] == "false" {
				if rand.Intn(10) <= 2 {
					continue
				}
			}
			tem_comm = append(tem_comm, one_comment)
		}
		batch_data.Comments = tem_comm
	}
	return batch_data
}

func format_output(true_times int, batch_data getMessage.DataDetail) MainData {
	t_record := VisitRecord{
		VisitTimes:   len(batch_data.Comments),
		SuccessTimes: true_times,
		SuccesScale:  Decimal(float64(true_times) / float64(len(batch_data.Comments)) * 100),
	}
	t_data := MainData{}
	t_data.DataDetail = batch_data
	t_data.VisitRecord = t_record
	return t_data
}
